package com.project.service.impl;

import java.util.Date;
import java.util.List;

import com.project.entity.ActualResults;
import com.project.entity.ActualResultsExample;
import com.project.entity.ProjectManagementExample;
import com.project.entity.ProjectMidCheck;
import com.project.mapper.ActualResultsMapper;
import com.project.mapper.ProjectManagementMapper;
import com.project.mapper.ProjectMidCheckMapper;
import com.project.mapper.UserMapper;
import com.project.service.ProjectMidCheckManage;
import com.project.util.ProjectManagementToProjectFee;


/**
 *中期检查服务类
 *作者：甘雨
 **/
public class ProjectMidCheckService implements ProjectMidCheckManage {
	//包含数据库操作类
	private ProjectMidCheckMapper projectMidCheckMapper;
	private ProjectManagementMapper projectManagementMapper;
	private ActualResultsMapper actualResultsMapper;
	private UserMapper userMapper;
	
	/**
	 * getter and
	 * setter~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	 **/
	
	public ProjectMidCheckMapper getProjectMidCheckMapper() {
		return projectMidCheckMapper;
	}

	public ProjectManagementMapper getProjectManagementMapper() {
		return projectManagementMapper;
	}

	public void setProjectManagementMapper(
			ProjectManagementMapper projectManagementMapper) {
		this.projectManagementMapper = projectManagementMapper;
	}

	public void setProjectMidCheckMapper(ProjectMidCheckMapper projectMidCheckMapper) {
		this.projectMidCheckMapper = projectMidCheckMapper;
	}

	public ActualResultsMapper getActualResultsMapper() {
		return actualResultsMapper;
	}

	public void setActualResultsMapper(ActualResultsMapper actualResultsMapper) {
		this.actualResultsMapper = actualResultsMapper;
	}

	public UserMapper getUserMapper() {
		return userMapper;
	}

	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	/**
	 *管理员对项目中期检查记录进行审核
	 *作者：甘雨
	 **/
	public void adminMidCheck(Integer actualResultsId) {
		try{
		ActualResults tmp = this.actualResultsMapper.selectByPrimaryKey(actualResultsId);

		
		tmp.setAuditmark("1");		
		this.actualResultsMapper.updateByPrimaryKey(tmp);//将实际结果记录中的审核字段置1
		ProjectMidCheck projectMidCheck = new  ProjectMidCheck();
		projectMidCheck.setActualresultsid(actualResultsId);
		ProjectManagementExample example = new ProjectManagementExample();
		example.or().andProjectnumberEqualTo(tmp.getProjectnumber());
		projectMidCheck.setFollowupperson(this.projectManagementMapper.selectByExample(example).get(0).getFollowupperson());
		projectMidCheck.setMidcheckdate(new Date()); 
		this.actualResultsMapper.insert(ProjectManagementToProjectFee.autoCreateActualResults(this.projectManagementMapper.selectByExample(example).get(0)));
		this.projectMidCheckMapper.insert(projectMidCheck);//添加管理员审查记录
		}catch(Exception e){
			System.out.println(e);
		}
	}

	/**
	 *管理员查看所有未审核的成果记录
	 *作者：甘雨
	 **/
	public List<ActualResults> getAllUncheck(Integer start , Integer limit) {
		ActualResultsExample example = new ActualResultsExample();
		example.or().andAuditmarkEqualTo("0");
		return this.actualResultsMapper.selectByExample(example,start,limit);
	}

	
	/**
	 *通过成果记录ID查找成果记录
	 *作者：甘雨
	 **/
	public ActualResults getById(Integer id){	
		return this.actualResultsMapper.selectByPrimaryKey(id);
	}

	
	/**
	 *通过项目编号查找成果记录
	 *作者：甘雨
	 **/
	public List<ActualResults> getByProjectNumber(String projectNumber) {
		ActualResultsExample example = new ActualResultsExample();
		example.createCriteria().andProjectnumberEqualTo(projectNumber);
		return this.actualResultsMapper.selectByExample(example,0,10);
	}

	public int userMidCheck(Integer userId, ActualResults actualResults) {
		List<ActualResults> actualResultsList = this.getByProjectNumber(this.userMapper.selectByPrimaryKey(userId).getProjectnumber());
		for(ActualResults tmp : actualResultsList){
			if(tmp.getAuditmark().equals("0")){
				ActualResultsExample example = new ActualResultsExample();
				example.createCriteria().andProjectnumberEqualTo(actualResults.getProjectnumber()).andAuditmarkEqualTo("0");
				this.actualResultsMapper.updateByExample(actualResults, example);
				return 0;
			}
		}
		this.actualResultsMapper.insert(actualResults);
		return 1;
	}
	
	
	public List<ActualResults> getByFollowUpId(Integer FollowUpId,String start , String limit){
		
		return this.actualResultsMapper.getByFollowUpId(FollowUpId, start, limit);
	}

}
